import 'dart:math';

import 'package:flutter_menu/biz/order/detail/option/options_controller.dart';
import 'package:flutter_menu/biz/order/widget/count_number_button.dart';
import 'package:flutter_menu/biz/order/widget/option_delete.dart';
import 'package:flutter_menu/model/product_model.dart';
import 'package:flutter_menu/res/app_design.dart';
import 'package:get/get.dart';

import 'option_item.dart';

class OptionSelector extends GetView<OptionsController> {
  const OptionSelector(
      {super.key, required this.groupId, this.title, required this.specList});

  final String? groupId;
  final String? title;
  final List<SpecOption> specList;

  bool isOthersMode() {
    return groupId?.isEmpty ?? true;
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OptionsController>(
      id: groupId,
      init: OptionsController(),
      builder: (OptionsController controller) {
        debugPrint("OptionsSelector Build: $groupId");
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 规格选择
            Row(
              children: [
                Text(title ?? "",
                    style: TextStyle(
                        fontSize: 26.sp, color: AppColors.text_CA7A2A)),
                const Spacer(),
                buildCountNumberButton()
              ],
            ),
            SizedBox(height: 20.w),
            specList
                .map((item) {
                  return buildOptionItem(item, controller);
                })
                .toList()
                .toWrap(
                  spacing: 25.0.w, // 水平方向的间距
                  runSpacing: 16.0.w, // 垂直方向的间距
                ),
            buildSelectedSpecListForOthersMode()
          ],
        );
      },
    );
  }

  Widget buildCountNumberButton() {
    if (isOthersMode()) {
      return const SizedBox();
    } else {
      var optionCountForNormalGroup =
          controller.optionCountForNormalGroup(groupId);
      debugPrint("optionCountForNormalGroup: $optionCountForNormalGroup");
      return CountNumberButton(
        initialValue: optionCountForNormalGroup,
        minValue: 1,
        maxValue: 9999,
        width: 124.w,
        height: 37.w,
        maxLength: 4,
        colorActive: AppColors.countNumberButtonActive,
        colorInactive: AppColors.countNumberButtonInactive,
        colorBg: AppColors.countNumberBg,
        textStyle: TextStyle(
            fontSize: 20.sp,
            color: AppColors.textInBottomSheet,
            fontWeight: AppStyle.semiBold),
        onChanged: (value, isAdd) {
          // Handle counter value changes
          controller.updateGroupOptionQuantity(groupId,
              quantity: value.toInt());
        },
      );
    }
  }

  Widget buildOptionItem(SpecOption item, OptionsController controller) {
    var name = item.getNameWithPrice();
    var id = item.id ?? "";
    return OptionItem(
      label: name,
      isEnable: true,
      isSelected: controller.isOptionSelected(groupId, id),
      onTap: () {
        // TODO 检查上限数量
        if (isOthersMode()) {
          controller.updateOptionQuantity(groupId, id, specOption: item);
        } else {
          if (!controller.isOptionSelected(groupId, id)) {
            controller.updateOptionQuantity(groupId, id, specOption: item);
          }
        }
      },
    );
  }

  buildSelectedSpecListForOthersMode() {
    if (!isOthersMode()) return const SizedBox.shrink();
    return Obx(() {
      var specQuantity = controller.quantity.value;
      return Padding(
        padding: const EdgeInsets.only(top: 6),
        child: controller
            .getSelectedOthersOptions()
            .map((item) {
          var optionQuantity = item.quantity * specQuantity;
          debugPrint("optionQuantity: $item $specQuantity");
          return OptionDelete(
            label: optionQuantity > 1
                ? "${item.specOption.getNameWithPrice()}x$optionQuantity"
                : item.specOption.getNameWithPrice(),
            onTap: () {
              controller.removeOptionQuantity(groupId, item.id);
            },
          ).paddingSymmetric(vertical: 7.w);
        })
            .toList()
            .toColumn(),
      );
    });

  }
}
